package cn.initcap.concurrency.concurrency.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;

/**
 * @author initcap
 * @date Created in 2018/6/18 PM3:48.
 */
@Slf4j
public class ThreadPoolLinkedBlockingQueue {

    private static final int THREAD_SIZE = 100;

    public static void main(String[] args) {
        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder().setNameFormat("ThreadPoolLinkedBlockingQueue").build();
        ExecutorService executorService = new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(), nameThreadFactory);

        for (int i = 0; i < THREAD_SIZE; i++) {
            final int index = i;
            executorService.execute(() -> {
                log.info("task:{}", index);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }

}
